//
//  LzwFirstDetailVC.m
//  NewHalfsugar
//
//  Created by 刘志武 on 16/1/7.
//  Copyright © 2016年 刘志武. All rights reserved.
//

#import "LzwFirstDetailVC.h"
#import "Block.h"
#import "LzwFirstWebViewVC.h"
#import "LzwbaseCollectView.h"
#import "LzwFirstDetailCollectCell.h"
#import "UMSocial.h"

@interface LzwFirstDetailVC ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) LzwbaseCollectView *myFirstCollectView;

@end

@implementation LzwFirstDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"值得一看";
    
    
    UIImage * LandRightBar = [UIImage imageNamed:@"return.png"];
    LandRightBar = [LandRightBar imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //设置分享按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:LandRightBar style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction:)];
    
    
       self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"99.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(ShareButtonAction:)];
    
    
    UICollectionViewFlowLayout *myFlowLayout = [[UICollectionViewFlowLayout alloc]init];
    //大小
    myFlowLayout.itemSize = CGSizeMake(150 * WIDTH, 200 * WIDTH);
    
    //距四周
    myFlowLayout.sectionInset = UIEdgeInsetsMake(20 * WIDTH, 20 * WIDTH, 20 * WIDTH, 20 * WIDTH);
    myFlowLayout.minimumLineSpacing = 20 * WIDTH;
    myFlowLayout.minimumInteritemSpacing = 20 * WIDTH;
    //上下滑动
    myFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
   
    self.myFirstCollectView = [[LzwbaseCollectView alloc]initWithFrame:[[UIScreen mainScreen]bounds] collectionViewLayout:myFlowLayout];
    
    [self.view addSubview:self.myFirstCollectView];
    
    self.myFirstCollectView.delegate = self;
    self.myFirstCollectView.dataSource = self;
    
    
    [self.myFirstCollectView registerClass:[LzwFirstDetailCollectCell class] forCellWithReuseIdentifier:@"cell"];
    
    self.arrayFirst = [NSMutableArray array];
    
    NSString *str = @"http://open3.bantangapp.com/product/list";
    
    NSDictionary *dic = @{@"app_installtime":@"1451365479.933367", @"app_versions":@"5.1.1", @"channel_name":@"appStore", @"client_id":@"bt_app_ios", @"client_secret":@"9c1e6634ce1c5098e056628cd66a17a5", @"device_token":@"c02c60075e7bab110fbdf27e15de3cb61e97360c4ea3545fd8a5936e46c28ccb", @"oauth_token":@"a41d3848f43102aa7759c27ba2c0fe8f", @"os_versions":@"9.1", @"page":@"0", @"pagesize":@"20", @"screensize":@"640", @"track_device_info":@"iPhone5%2C3", @"track_deviceid":@"A80585E3-D6AD-4A53-BE1C-5963901C96E5", @"track_user_id":@"1746436", @"v":@"9"};
    
    
    [Block creatingDataTask:str parameters:dic Cookie:nil User_Agent:nil successAFNBlock:^(id resuposeObject) {
        
        NSMutableDictionary *dic = [resuposeObject objectForKey:@"data"];
        
        NSMutableArray *array = [dic objectForKey:@"product"];
        
        for (NSMutableDictionary *dict in array) {
            
            LzwFirstDeModle *modle = [[LzwFirstDeModle alloc]init];
            
            [modle setValuesForKeysWithDictionary:dict];
            
            [self.arrayFirst addObject:modle];
            
            
        }
        [self.myFirstCollectView reloadData];
        
    }];
    
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.arrayFirst.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LzwFirstDetailCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.modleFirst = [self.arrayFirst objectAtIndex:indexPath.row];
    
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LzwFirstWebViewVC *webVC = [[LzwFirstWebViewVC alloc]init];
    
    webVC.modleWebVc = [self.arrayFirst objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:webVC animated:YES];
    
}

- (void)leftButtonAction:(UIBarButtonItem *)bar{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}

- (void)ShareButtonAction:(UIBarButtonItem *)bar{
    
    //注意：分享到微信好友、微信朋友圈、微信收藏、QQ空间、QQ好友、来往好友、来往朋友圈、易信好友、易信朋友圈、Facebook、Twitter、Instagram等平台需要参考各自的集成方法
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"56911ce367e58e117a002ed8"
                                      shareText:@"请输入您要分享的文字......"                                     shareImage:nil
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina, UMShareToRenren, UMShareToTencent,  nil]
                                       delegate:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
